package com.jzx.provider.cv.service;

import com.alibaba.druid.support.json.JSONUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hankcs.hanlp.HanLP;
import com.jzx.provider.common.utils.BeanUtil;
import com.jzx.provider.common.utils.DateUtil;
import com.jzx.provider.common.utils.ListUtil;
import com.jzx.provider.cv.constants.Education;
import com.jzx.provider.cv.constants.MaritalStatus;
import com.jzx.provider.cv.dto.command.*;
import com.jzx.provider.cv.dto.result.*;
import com.jzx.provider.cv.model.*;
import com.jzx.provider.cv.repository.*;
import com.jzx.provider.cv.utils.IdWorker;
import com.jzx.provider.exception.CvRuntimeException;
import com.jzx.provider.exception.MyErrorType;
import com.jzx.provider.ucenter.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author daihuihuiui
 * @date 2019/2/5
 * @des 简历业务逻辑类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CvService {


    @Autowired
    private CvRepository cvRepository;

    @Autowired
    private CurrentInfoRepository currentInfoRepository;

    @Autowired
    private ForwardInfoRepository forwardInfoRepository;

    @Autowired
    private EducationInfoRepository educationInfoRepository;

    @Autowired
    private WorkInfoRepository workInfoRepository;

    @Autowired
    private ProjectInfoRepository projectInfoRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private CvIndexService cvIndexService;

    @Autowired
    private CerInfoRepository cerInfoRepository;

    @Autowired
    private IdWorker idWorker;

    /**
     * 编辑或修改简历
     */
    public long saveOrUpdate(CvCommand cvCommand, String token) {
        if (!StringUtils.isEmpty(cvCommand.getId())) {
            cvRepository.findById(cvCommand.getId()).ifPresent(cvRes -> {
                //判断是否本人修改简历
                if (!cvRes.getUserId().equals(userService.findUserIdBytoken(token))) {
                    throw new CvRuntimeException(MyErrorType.NOT_ALLOW);
                }
                BeanUtil.copyPropertiesIgnoreEmpty(cvCommand, cvRes);
                cvRepository.save(cvRes);
            });
        } else {
            Cv cv = Cv.builder().build();
            BeanCopier.create(CvCommand.class, Cv.class, false)
                    .copy(cvCommand, cv, null);
            //生成简历id
            cv.setId(idWorker.nextId());
            //绑定userId
            cv.setUserId(userService.findUserIdBytoken(token));
            cvRepository.save(cv);
            return cv.getId();
        }
        return cvCommand.getId();
    }

    public void saveOrUpdateCurrentInfo(CurrentInfoCommand currentInfoCommand) {
        if (StringUtils.isEmpty(currentInfoCommand.getId())) {
            throw new CvRuntimeException(MyErrorType.CV_ID_MISS);
        }
        Optional<CurrentInfo> optional = currentInfoRepository.findById(currentInfoCommand.getId());
        CurrentInfo currentInfo = optional.get();
        if (null != currentInfo) {
            BeanCopier.create(CurrentInfoCommand.class, CurrentInfo.class, false)
                    .copy(currentInfoCommand, currentInfo, null);
            currentInfoRepository.save(currentInfo);
        }
        cvRepository.findById(currentInfo.getCvId()).ifPresent(cv -> {
            cv.setUpdateTime(new Date());
            cv.setIsIndex(0);
            cvRepository.save(cv);
        });
    }

    public void saveOrUpdateForwardInfo(ForwardInfoCommand forwardInfoCommand) {
        if (StringUtils.isEmpty(forwardInfoCommand.getCvId())) {
            throw new CvRuntimeException(MyErrorType.CV_ID_MISS);
        }
        cvRepository.findById(forwardInfoCommand.getCvId()).ifPresent(cv -> {
            ForwardInfo forwardInfo = ForwardInfo.builder().build();
            BeanCopier.create(ForwardInfoCommand.class, ForwardInfo.class, false)
                    .copy(forwardInfoCommand, forwardInfo, null);
            cv.setUpdateTime(new Date());
            cv.setIsIndex(0);
            cvRepository.save(cv);
        });
    }


    /**
     * 根据用户id查询所属简历
     *
     * @param userId
     * @return
     */
    public List<Long> findCvIdByUserId(Integer userId) {
        List<Long> cvIds = new ArrayList<>();
        cvRepository.findAll(Example.of(Cv.builder().userId(userId).build())).forEach(item -> {
            cvIds.add(item.getId());
        });
        return cvIds;
    }

    /**
     * 简历录入
     * 添加 简历相关 工作经验 教育经历 项目经历 当前情况 期望 职位 证书 等
     *
     * @param addCvCommand
     * @return
     */
    public void addCv(AddCvCommand addCvCommand) {
        CvCommand cvCommand = addCvCommand.getCv();
        if (null == cvCommand || org.apache.commons.lang3.StringUtils.isBlank(cvCommand.getName()) ||
                org.apache.commons.lang3.StringUtils.isBlank(cvCommand.getTel())) {
            throw new CvRuntimeException(MyErrorType.NAME_TEL_NOT_ALLOW_NULL);
        }
        Long cvId = cvCommand.getId();
        Cv cv = Cv.builder().build();
        if (null == cvId) {
            cvId = idWorker.nextId();
        }
        BeanCopier.create(CvCommand.class, Cv.class, false).copy(cvCommand, cv, null);
        //简历刚录入，所有都未验证
        cv.setIsCheck("000");
        cv.setId(cvId);
        cv.setIsIndex(0);
        cv.setCreateTime(new Date());
        cv.setUpdateTime(new Date());
        cvRepository.save(cv);
        if (null != addCvCommand.getCurrentInfo()) {
            CurrentInfoCommand currentInfoCommand = addCvCommand.getCurrentInfo();
            CurrentInfo currentInfo = CurrentInfo.builder().build();
            BeanCopier.create(CurrentInfoCommand.class, CurrentInfo.class, false).copy(currentInfoCommand, currentInfo, null);
            currentInfo.setCvId(cvId);
            currentInfo.setSalary(currentInfoCommand.getCurrentSalaryPerMonth() + "*" + currentInfoCommand.getCurrentSalaryMonths() + ",1");
            currentInfo.setCreateTime(new Date());
            currentInfo.setUpdateTime(new Date());
            currentInfoRepository.save(currentInfo);
        }

        if (null != addCvCommand.getForwardInfo()) {
            ForwardInfoCommand forwardInfoCommand = addCvCommand.getForwardInfo();
            ForwardInfo forwardInfo = ForwardInfo.builder().build();
            BeanCopier.create(ForwardInfoCommand.class, ForwardInfo.class, false).copy(forwardInfoCommand, forwardInfo, null);
            forwardInfo.setCvId(cvId);
            forwardInfo.setSalary(forwardInfoCommand.getForwardSalaryPerMonth() + "*" + forwardInfoCommand.getForwardSalaryMonths() + ",1");
            forwardInfo.setCreateTime(new Date());
            forwardInfo.setUpdateTime(new Date());
            forwardInfoRepository.save(forwardInfo);
        }

        if (!ListUtil.isEmpty(addCvCommand.getEducationInfoList())) {
            List<EducationInfoCommand> educationInfoCommandList = addCvCommand.getEducationInfoList();
            List<EducationInfo> educationInfoList = new ArrayList<>(10);
            for (EducationInfoCommand educationInfoCommand : educationInfoCommandList) {
                EducationInfo educationInfo = EducationInfo.builder().build();
                BeanCopier.create(EducationInfoCommand.class, EducationInfo.class, false).copy(educationInfoCommand, educationInfo, null);
                educationInfo.setCvId(cvId);
                if (!"至今".equals(educationInfoCommand.getStartDate())) {
                    try {
                        educationInfo.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(educationInfoCommand.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                    } catch (Exception e) {
                    }
                }
                if (!"至今".equals(educationInfoCommand.getEndDate())) {
                    try {
                        educationInfo.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(educationInfoCommand.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                    } catch (Exception e) {
                    }
                }
                //判断211、985
                HanLP.segment(educationInfoCommand.getSchool()).forEach(term -> {
                    if (term.nature.toString().equals("school1")) {
                        educationInfo.setIsKeySchool(1);
                    } else if (term.nature.toString().equals("school2")) {
                        educationInfo.setIsKeySchool(2);
                    } else if (term.nature.toString().equals("school")) {
                        educationInfo.setIsKeySchool(0);
                    } else {
                        educationInfo.setIsKeySchool(0);
                    }
                });
                educationInfo.setCreateTime(new Date());
                educationInfo.setUpdateTime(new Date());
                educationInfoList.add(educationInfo);
            }
            //排序
            educationInfoList.sort((EducationInfo edu1, EducationInfo edu2) -> DateUtil.strToDate(edu2.getStartDate(), "yyyy-MM").compareTo(DateUtil.strToDate(edu1.getStartDate(), "yyyy-MM")));
            educationInfoRepository.saveAll(educationInfoList);
        }

        if (!ListUtil.isEmpty(addCvCommand.getProjectInfoList())) {
            List<ProjectInfoCommand> projectInfoCommandList = addCvCommand.getProjectInfoList();
            List<ProjectInfo> projectInfoList = new ArrayList<>(10);
            for (ProjectInfoCommand projectInfoCommand : projectInfoCommandList) {
                ProjectInfo projectInfo = ProjectInfo.builder().build();
                BeanCopier.create(ProjectInfoCommand.class, ProjectInfo.class, false).copy(projectInfoCommand, projectInfo, null);
                projectInfo.setCvId(cvId);
                if (!"至今".equals(projectInfoCommand.getStartDate())) {
                    projectInfo.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(projectInfoCommand.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                }
                if (!"至今".equals(projectInfoCommand.getEndDate())) {
                    projectInfo.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(projectInfoCommand.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                }
                projectInfo.setCreateTime(new Date());
                projectInfo.setUpdateTime(new Date());
                projectInfoList.add(projectInfo);
            }
            //排序
            projectInfoList.sort((ProjectInfo pro1, ProjectInfo pro2) -> DateUtil.strToDate(pro2.getStartDate(), "yyyy-MM").compareTo(DateUtil.strToDate(pro1.getStartDate(), "yyyy-MM")));
            projectInfoRepository.saveAll(projectInfoList);
        }

        if (!ListUtil.isEmpty(addCvCommand.getWorkInfoList())) {
            List<WorkInfoCommand> workInfoCommandList = addCvCommand.getWorkInfoList();
            List<WorkInfo> workInfoList = new ArrayList<>();
            for (WorkInfoCommand workInfoCommand : workInfoCommandList) {
                WorkInfo workInfo = WorkInfo.builder().build();
                if (null != workInfo.getId()) {
                    workInfo = workInfoRepository.getOne(workInfo.getId());
                }
                BeanCopier.create(WorkInfoCommand.class, WorkInfo.class, false).copy(workInfoCommand, workInfo, null);
                workInfo.setCvId(cvId);
                if (!"至今".equals(workInfo.getStartDate())) {
                    workInfo.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(workInfo.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                }
                if (!"至今".equals(workInfo.getEndDate())) {
                    workInfo.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(workInfo.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
                }
                workInfo.setVocation(String.join("|", workInfoCommand.getVocation()));
                workInfo.setCreateTime(new Date());
                workInfo.setUpdateTime(new Date());
                workInfoList.add(workInfo);
            }
            //排序
            workInfoList.sort((WorkInfo work1, WorkInfo work2) -> DateUtil.strToDate(work2.getStartDate(), "yyyy-MM").compareTo(DateUtil.strToDate(work1.getStartDate(), "yyyy-MM")));
            //更新开始工作日期
            cvRepository.save(cv);
            workInfoRepository.saveAll(workInfoList);
            //跟新当前状况
            CurrentInfo currentInfo = currentInfoRepository.findByCvId(cvId);
        }
        if (!ListUtil.isEmpty(addCvCommand.getCerInfoList())) {
            List<CerInfoCommand> cerInfoCommandList = addCvCommand.getCerInfoList();
            List<CerInfo> cerInfoList = new ArrayList<>(10);
            for (CerInfoCommand cerInfoCommand : cerInfoCommandList) {
                CerInfo cerInfo = CerInfo.builder().build();
                if (null != cerInfoCommand.getId()) {
                    cerInfo = cerInfoRepository.getOne(cerInfoCommand.getId());
                }
                cerInfo.setCvId(cvId);
                cerInfo.setGetDate(cerInfoCommand.getGetDate());
                cerInfo.setName(cerInfoCommand.getName());
                cerInfoList.add(cerInfo);
            }
            cerInfoRepository.saveAll(cerInfoList);
        }
    }


    /**
     * 获取简历详情
     *
     * @param id
     * @return
     */
    public CvResultForNoContact findCvById(long id, List<String> highlight) {
        CvResultForNoContact cvResultForNoContact = CvResultForNoContact.builder().build();
        cvRepository.findById(id).ifPresent(cv -> {
            BeanCopier.create(Cv.class, CvResultForNoContact.class, false)
                    .copy(cv, cvResultForNoContact, null);
            cvResultForNoContact.setMaritalStatus(MaritalStatus.getByValue(cv.getMaritalStatus()));
            cvResultForNoContact.setSex(cv.getSex() == 2 ? "女" : "男");
            cvResultForNoContact.setName("******");
            cvResultForNoContact.setTel("******");
            cvResultForNoContact.setEmail("*******");
            //计算年龄
            if (cv.getBirthDay() != null) {
                try {
                    cvResultForNoContact.setAge(DateUtil.getYearByDate(cv.getBirthDay()));
                } catch (Exception e) {
                }
            }
        });

        //获取当前状况
        CurrentInfo currentInfo = currentInfoRepository.findByCvId(id);
        if (currentInfo != null) {
            if (StringUtils.isEmpty(currentInfo.getSalary())) {
                cvResultForNoContact.setSalary("保密");
            } else {
                String[] salary = currentInfo.getSalary().split(",", 2);
                String[] salary0 = salary[0].split("\\*", 2);
                if (StringUtils.isEmpty(salary0[0])) {
                    cvResultForNoContact.setSalary("保密");
                } else {
                    DecimalFormat df1 = new DecimalFormat("0.00");
                    cvResultForNoContact.setSalary(df1.format(Double.valueOf(salary0[0]) * Double.valueOf(salary0[1]) / 10000) + "万");
                }
            }
            cvResultForNoContact.setLocation(currentInfo.getNowLocation());
            if (currentInfo.getSwitchStatus() == null) {
                cvResultForNoContact.setSwitchStatus("看机会");
            } else {
                cvResultForNoContact.setSwitchStatus(currentInfo.getSwitchStatus() == 1 ? "看机会" : "不看机会");
            }
        }

        //获取期望情况
        ForwardInfo forwardInfo = forwardInfoRepository.findByCvId(id);
        if (forwardInfo != null) {
            cvResultForNoContact.setForwardCities(forwardInfo.getCities());
            if (StringUtils.isEmpty(forwardInfo.getSalary())) {
                cvResultForNoContact.setForwardSalary("面议");
            } else {
                String[] salary = forwardInfo.getSalary().split(",", 2);
                String[] salary0 = salary[0].split("\\*", 2);
                if(StringUtils.isEmpty(salary0[0])){
                    cvResultForNoContact.setForwardSalary("面议");
                }else {
                    DecimalFormat df1 = new DecimalFormat("0.00");
                    cvResultForNoContact.setForwardSalary(df1.format(Double.valueOf(salary0[0]) * Double.valueOf(salary0[1]) / 10000) + "万");
                }
            }
            cvResultForNoContact.setForwardTitles(forwardInfo.getTitles());
            cvResultForNoContact.setForwardVocations(forwardInfo.getVocations().replace("|", ","));
        }

        List<EducationInfo> educationInfos = educationInfoRepository.findAll(Example.of(EducationInfo.builder().cvId(id).build()));
        cvResultForNoContact.setEdu(Education.getByValue(educationInfos.get(0).getEducation()));
        educationInfos.forEach(edu -> {
            EducationInfoResultForNoContact educationInfoResultForNoContact = EducationInfoResultForNoContact.builder().build();
            BeanCopier.create(EducationInfo.class, EducationInfoResultForNoContact.class, false)
                    .copy(edu, educationInfoResultForNoContact, null);
            educationInfoResultForNoContact.setEducation(Education.getByValue(edu.getEducation()));
            if (edu.getIsRecruitment() == null) {
                educationInfoResultForNoContact.setIsRecruitment("否");
            } else {
                educationInfoResultForNoContact.setIsRecruitment(edu.getIsRecruitment() == 1 ? "是" : "否");
            }
            cvResultForNoContact.getEdus().add(educationInfoResultForNoContact);
        });
        List<WorkInfo> workInfos = workInfoRepository.findAll(Example.of(WorkInfo.builder().cvId(id).build()));
        if (workInfos != null && workInfos.size() > 0) {
            //计算工作年限
            cvResultForNoContact.setWorkYear(DateUtil.getWorkYear(workInfos.get(workInfos.size() - 1).getStartDate()) + "");
            if (!StringUtils.isEmpty(workInfos.get(0).getVocation())) {
                cvResultForNoContact.setVocation(workInfos.get(0).getVocation().replace("|", ","));
            }
            cvResultForNoContact.setTitle(workInfos.get(0).getTitle());
            cvResultForNoContact.setCom(workInfos.get(0).getCompany());
            workInfos.forEach(work -> {
                WorkInfoResultForNoContact workInfoResultForNoContact = WorkInfoResultForNoContact.builder().build();
                BeanCopier.create(WorkInfo.class, WorkInfoResultForNoContact.class, false)
                        .copy(work, workInfoResultForNoContact, null);
                cvResultForNoContact.getWorks().add(workInfoResultForNoContact);
            });
        }
        projectInfoRepository.findAll(Example.of(ProjectInfo.builder().cvId(id).build())).forEach(project -> {
            ProjectInfoResult projectInfoResult = ProjectInfoResult.builder().build();
            BeanCopier.create(ProjectInfo.class, ProjectInfoResult.class, false)
                    .copy(project, projectInfoResult, null);
            cvResultForNoContact.getProjects().add(projectInfoResult);
        });
        cerInfoRepository.findAll(Example.of(CerInfo.builder().cvId(id).build())).forEach(cer -> {
            CerInfoResult cerInfoResult = CerInfoResult.builder().build();
            BeanCopier.create(CerInfo.class, CerInfoResult.class, false)
                    .copy(cer, cerInfoResult, null);
            cvResultForNoContact.getCertificates().add(cerInfoResult);
        });
        Gson gson = new GsonBuilder().create();
        String res = gson.toJson(cvResultForNoContact);
        for (String key : highlight) {
            res = cvIndexService.IgnoreCaseReplace(res, key);
        }
        return gson.fromJson(res, CvResultForNoContact.class);
    }

    /**
     * 根据姓名 手机查询简历
     *
     * @param name
     * @param mobile
     * @return
     */
    public CvResult findCvByNameAndMobile(String name, String mobile) {
        CvResult cvResult = CvResult.builder().build();
        List<Cv> cvList = cvRepository.findAll(Example.of(Cv.builder().name(name).tel(mobile).build()));
        if (null != cvList && cvList.size() > 0) {
            Cv cv = cvList.get(0);
            BeanCopier.create(Cv.class, CvResult.class, false)
                    .copy(cv, cvResult, null);
            cvResult.setSid(cv.getId() + "");
            educationInfoRepository.findAll(Example.of(EducationInfo.builder().cvId(cvResult.getId()).build())).forEach(edu -> {
                EducationInfoResult educationInfoResult = EducationInfoResult.builder().build();
                BeanCopier.create(EducationInfo.class, EducationInfoResult.class, false)
                        .copy(edu, educationInfoResult, null);
                cvResult.getEducationInfoList().add(educationInfoResult);
            });
            workInfoRepository.findAll(Example.of(WorkInfo.builder().cvId(cvResult.getId()).build())).forEach(work -> {
                WorkInfoResult workInfoResult = WorkInfoResult.builder().build();
                BeanCopier.create(WorkInfo.class, WorkInfoResult.class, false)
                        .copy(work, workInfoResult, null);
                if (!StringUtils.isEmpty(work.getVocation())) {
                    workInfoResult.setVocation(Arrays.asList(work.getVocation().split("\\|")));
                }
                cvResult.getWorkInfoList().add(workInfoResult);
            });
            projectInfoRepository.findAll(Example.of(ProjectInfo.builder().cvId(cvResult.getId()).build())).forEach(projectInfo -> {
                ProjectInfoResult projectInfoResult = ProjectInfoResult.builder().build();
                BeanCopier.create(ProjectInfo.class, ProjectInfoResult.class, false)
                        .copy(projectInfo, projectInfoResult, null);
                cvResult.getProjectInfoList().add(projectInfoResult);
            });
            cerInfoRepository.findAll(Example.of(CerInfo.builder().cvId(cvResult.getId()).build())).forEach(cerInfo -> {
                CerInfoResult cerInfoResult = CerInfoResult.builder().build();
                BeanCopier.create(CerInfo.class, CerInfoResult.class, false)
                        .copy(cerInfo, cerInfoResult, null);
                cvResult.getCerInfoList().add(cerInfoResult);
            });
            List<CurrentInfo> CurrentInfoList = currentInfoRepository.findAll(Example.of(CurrentInfo.builder().cvId(cvResult.getId()).build()));
            if (null != CurrentInfoList && CurrentInfoList.size() > 0) {
                CurrentInfoResult currentInfoResult = CurrentInfoResult.builder().build();
                BeanCopier.create(CurrentInfo.class, CurrentInfoResult.class, false)
                        .copy(CurrentInfoList.get(0), currentInfoResult, null);
                if (!StringUtils.isEmpty(CurrentInfoList.get(0).getSalary())) {
                    String[] salary = CurrentInfoList.get(0).getSalary().split(",", 2);
                    String[] salary0 = salary[0].split("\\*", 2);
                    currentInfoResult.setCurrentSalaryPerMonth(salary0[0]);
                    currentInfoResult.setCurrentSalaryMonths(salary0[1]);
                }
                cvResult.setCurrentInfo(currentInfoResult);
            }
            List<ForwardInfo> ForwardInfoList = forwardInfoRepository.findAll(Example.of(ForwardInfo.builder().cvId(cvResult.getId()).build()));
            if (null != ForwardInfoList && ForwardInfoList.size() > 0) {
                ForwardInfoResult forwardInfoResult = ForwardInfoResult.builder().build();
                BeanCopier.create(ForwardInfo.class, ForwardInfoResult.class, false)
                        .copy(ForwardInfoList.get(0), forwardInfoResult, null);
                if (!StringUtils.isEmpty(ForwardInfoList.get(0).getSalary())) {
                    String[] salary = ForwardInfoList.get(0).getSalary().split(",", 2);
                    String[] salary0 = salary[0].split("\\*", 2);
                    forwardInfoResult.setForwardSalaryPerMonth(salary0[0]);
                    forwardInfoResult.setForwardSalaryMonths(salary0[1]);
                }
                cvResult.setForwardInfo(forwardInfoResult);
            }
            return cvResult;
        }
        return null;

    }

    /**
     * 更新简历与当前状况
     * 因为ui将部分简历内容与当前情况展示在一起  所以也将两个表的更新放在一个接口里
     *
     * @param updateCvAndCurrentCommand
     */
    public void updateCvAndCurrent(UpdateCvAndCurrentCommand updateCvAndCurrentCommand) {
        //更新cv表
        if (null != updateCvAndCurrentCommand.getCvCommand()) {
            CvCommand cvCommand = updateCvAndCurrentCommand.getCvCommand();
            if (null != cvCommand.getId()) {
                Cv cv = Cv.builder().isIndex(0).build();
                //更新个人简介
                if (!StringUtils.isEmpty(cvCommand.getPersonal())) {
                    BeanCopier.create(CvCommand.class, Cv.class, false)
                            .copy(cvCommand, cv, null);
                    cv.setUpdateTime(new Date());
                    cv.setIsIndex(0);
                    cvRepository.updateCvPersonal(cv);
                } else if (!StringUtils.isEmpty(cvCommand.getAdditional())) {
                    BeanCopier.create(CvCommand.class, Cv.class, false)
                            .copy(cvCommand, cv, null);
                    cv.setUpdateTime(new Date());
                    cv.setIsIndex(0);
                    cvRepository.updateCvAdditional(cv);
                } else if (!StringUtils.isEmpty(cvCommand.getProfessional())) {
                    BeanCopier.create(CvCommand.class, Cv.class, false)
                            .copy(cvCommand, cv, null);
                    cv.setUpdateTime(new Date());
                    cv.setIsIndex(0);
                    cvRepository.updateCvProfessional(cv);
                } else {//更新基本信息
                    BeanCopier.create(CvCommand.class, Cv.class, false)
                            .copy(cvCommand, cv, null);
                    cv.setUpdateTime(new Date());
                    cv.setIsIndex(0);
                    cvRepository.updateCvBasicInfo(cv);
                }
            }
        }
        //更新current表
        if (null != updateCvAndCurrentCommand.getCurrentInfoCommand()) {
            CurrentInfoCommand currentInfoCommand = updateCvAndCurrentCommand.getCurrentInfoCommand();
            if (null != currentInfoCommand.getId()) {
                CurrentInfo currentInfo = CurrentInfo.builder().build();
                BeanCopier.create(CurrentInfoCommand.class, CurrentInfo.class, false)
                        .copy(currentInfoCommand, currentInfo, null);
                currentInfo.setSalary(currentInfoCommand.getCurrentSalaryPerMonth() + "*" + currentInfoCommand.getCurrentSalaryMonths() + ",1");
                currentInfo.setUpdateTime(new Date());
                currentInfoRepository.updateCvCurrentInfo(currentInfo);

                cvRepository.findById(updateCvAndCurrentCommand.getCvCommand().getId()).ifPresent(cv->{
                    cv.setUpdateTime(new Date());
                    cv.setIsIndex(0);
                    cvRepository.updateCvBasicInfo(cv);
                });
            }
        }
    }

    public void updateCvPersonal(UpdateCvAndCurrentCommand updateCvAndCurrentCommand) {
        if (null != updateCvAndCurrentCommand.getCvCommand()) {
            CvCommand cvCommand = updateCvAndCurrentCommand.getCvCommand();
            if (null != cvCommand.getId()) {
                Cv cv = Cv.builder().build();
                BeanCopier.create(CvCommand.class, Cv.class, false)
                        .copy(cvCommand, cv, null);
                cv.setUpdateTime(new Date());
                cvRepository.updateCvPersonal(cv);
            }
        }
    }


    /**
     * 更新期望
     *
     * @param forwardInfoCommand
     */
    public void updateForwardInfo(ForwardInfoCommand forwardInfoCommand) {
        if (null != forwardInfoCommand.getId()) {
            ForwardInfo forwardInfo = ForwardInfo.builder().build();
            BeanCopier.create(ForwardInfoCommand.class, ForwardInfo.class, false).copy(forwardInfoCommand, forwardInfo, null);
            forwardInfo.setSalary(forwardInfoCommand.getForwardSalaryPerMonth() + "*" + forwardInfoCommand.getForwardSalaryMonths() + ",1");
            forwardInfo.setCreateTime(new Date());
            forwardInfoRepository.updateCvForwardInfo(forwardInfo);
        }
    }

    /**
     * 更新证书
     *
     * @param cerInfoCommand
     */
    public void updateCerInfo(CerInfoCommand cerInfoCommand) {
        if (null != cerInfoCommand.getId()) {
            cerInfoRepository.findById(cerInfoCommand.getId()).ifPresent(cerInfo -> {
                BeanUtil.copyPropertiesIgnoreEmpty(cerInfoCommand, cerInfo);
                cerInfoRepository.save(cerInfo);
            });
        } else {
            CerInfo cerInfo = CerInfo.builder().build();
            BeanCopier.create(CerInfoCommand.class, CerInfo.class, false).copy(cerInfoCommand, cerInfo, null);
            cerInfoRepository.save(cerInfo);
        }
    }

    /**
     * 删除证书
     *
     * @param id
     */
    public void deleteCer(Integer id) {
        cerInfoRepository.findById(id).ifPresent(cer -> {
            cerInfoRepository.deleteById(id);
            //更新简历时间
            cvRepository.findById(cer.getCvId()).ifPresent(cv -> {
                cv.setUpdateTime(new Date());
                cvRepository.save(cv);
                //更新es索引
            });
        });
    }

}